package com.pcbly.web.action.system;

import java.util.Date;
import java.util.List;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.system.ProvinceCityBean;
import com.pcbly.web.business.system.ProvinceCityService;
import com.pcbly.web.components.hibernate.support.Page;

/**
* @author carl.song
* Copyright (c) 2010 Lightsoft, International.
* All rights reserved.
*
*/

public class ProvinceCityAction extends BaseAction<ProvinceCityBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8863182861703639448L;

	protected ProvinceCityService provinceCityService; 
	
	public void setProvinceCityService(ProvinceCityService provinceCityService) {
		this.provinceCityService = provinceCityService;
	}
	private ProvinceCityBean provinceCity;
	
	private String orderBy;
	
	private Page<ProvinceCityBean> pages;
	
	private List<ProvinceCityBean> provinceCityBeans;
	
	private boolean asc = true;
	
	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}
	
	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	public ProvinceCityBean getProvinceCity() {
		return provinceCity;
	}
	public Page<ProvinceCityBean> getPages() {
		return pages;
	}

	public void setPages(Page<ProvinceCityBean> pages) {
		this.pages = pages;
	}
	public void setProvinceCity(ProvinceCityBean provinceCity) {
		this.provinceCity = provinceCity;
	}
	public String view() throws Exception {
		long startTime = System.currentTimeMillis();
		provinceCity=provinceCityService.getProvinceCityBeanById(getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.view();
	}
	public String update() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		provinceCity=provinceCityService.getProvinceCityBeanById(getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.update();
	}
	public String index() throws Exception {
		long startTime = System.currentTimeMillis();

		long start = getPage();
		start = start * getPageSize();
		
//		if (orderBy == null || "".equals(orderBy)) {
//			orderBy = " id ";
//		}
//		pages = new Page<ProvinceCityBean>();
//		pages.setStart(start);
//		pages.setPageSize(getPageSize());
//		pages = provinceCityService.findProvinceCityBeanByCond(
//				provinceCity, pages, orderBy, asc);
		
		provinceCityBeans=provinceCityService.findProvinceCityBeansByParentId("0");
		
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("page:::::" + getPage());
			log.debug("pageSize:::::" + getPageSize());
			log.debug("time ms:::::" + startTime);

			System.out.println("page:::::" + getPage());
			System.out.println("pageSize:::::" + getPageSize());
			System.out.println("time ms:::::" + startTime);
		}
		return super.index();
	}
	public String add() throws Exception { 
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		provinceCity=new ProvinceCityBean();
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.add();
	}
	public String save() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(provinceCity.getId());
		}
		try{
		if (provinceCity.getId() == null || "".equals(provinceCity.getId().trim())) {// new 
			provinceCity.setCreateDate(new Date());
			provinceCity.setCreateUserId(getUserId());
			provinceCity.setRecycled("0");
			provinceCityService.saveProvinceCityBean(provinceCity);
			setMyMessage(getText("entity.saved"));
		} else {// update
			provinceCityService.updateProvinceCityBean(provinceCity);
			setMyMessage(getText("entity.updated"));
		}}catch(Exception e){
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.save();
	}
	public String dwrSave(ProvinceCityBean bean) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		try{
		if (bean.getId() == null || "".equals(bean.getId())) {// new 
			provinceCityService.saveProvinceCityBean(bean);
		} else {// update
			provinceCityService.updateProvinceCityBean(bean);
		}}catch(Exception e){
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseSave(bean.getId());
	}
	public String delete() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(getId());
		}
		if(getId().indexOf(",")>=0){
			provinceCityService.removeProvinceCityBeanByIds(getId());
		}else{
			provinceCityService.removeProvinceCityBeanById(getId());
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		provinceCity=null;
		return super.delete();
	}
	public String dwrDelete(String id) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(id);
		}
		if(id.indexOf(",")>=0){
			provinceCityService.removeProvinceCityBeanByIds(id);
		}else{
			provinceCityService.removeProvinceCityBeanById(id);
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseDelete(id);
	}

	public List<ProvinceCityBean> getProvinceCityBeans() {
		return provinceCityBeans;
	}

	public void setProvinceCityBeans(List<ProvinceCityBean> provinceCityBeans) {
		this.provinceCityBeans = provinceCityBeans;
	}
	
}